<?php

namespace App\Http\Model;

use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class ColorGift extends Base
{
    protected $colorGiftPoolTable = 'color_gift_pool';
    protected $colorGiftPoolConfigTable = 'color_gift_pool_config';
    protected $colorGiftPoolRateBannerTable = 'color_gift_pool_rate_banner';
    protected $wOrderTable = 'color_gift_rate_award_stream';
    protected $wDrawTable = 'wish_wheel_draw';
    protected $wWhiteTable = 'wish_wheel_white';
    protected $wStatisticsTable = 'wish_wheel_statistics';
    protected $colorWhiteConfig = 'color_gift_white_config';//白名单用户

    //彩蛋配置缓存
    protected $colorGiftConfigKey = 'COLORGIFT:CONFIG';//string COLORGIFT:CONFIG => {string}
    //彩蛋奖池总缓存
    protected $colorGiftPoolKey = 'COLORGIFT:POOL';//string COLORGIFT:POOL => {string}
    //彩蛋奖池缓存
    protected $colorGiftPoolByGiftIdKey = 'COLORGIFT:POOL:GIFTID:';//string COLORGIFT:POOL:GIFTID: => {string}
    //彩蛋奖池配置缓存
    protected $colorGiftPoolConfigKey = 'COLORGIFT:POOLCONFIG:';//string COLORGIFT:POOLCONFIG:{poolId} => {string}
    //彩蛋奖池配置数据缓存
    protected $colorGiftPoolDataKey = '{COLORGIFT:POOLDATA:%s}'; //奖池 {COLORGIFT:POOLDATA:{poolId}}

    //用户抽奖数
    protected $userDrawNumKey = 'COLORGIFT:USERDRAW:NUM:';//string COLORGIFT:USERDRAW:NUM:{userId} => {num}

    //活动彩蛋缓存
    protected $activityColorConfig = 'ACTIVITY:COLOR:GIFT:POOLRATE';

    protected $colorGiftPoolRateNum = 'COLOR:GIFT:ROUND:POOL:';//对应奖池下的概率次数缓存 COLOR:GIFT:ROUND:POOL:{poolid}

    //彩蛋概率下的banner缓存
    protected $colorGiftRateBanner = 'COLOR:GIFT:BANNER:';//string COLOR:GIFT:BANNER:{pool_id}:{rate_num}


    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    //编辑彩蛋配置
    public function editColorGiftConfig($params)
    {
        $upData = [];
        if(!empty($params['need_num'])) {
            $upData['need_num'] = $params['need_num'];
        }
        if(isset($params['new_num'])) {
            $upData['new_num'] = $params['new_num'];
        }
        if(!empty($params['draw_num_1'])) {
            $upData['draw_num_1'] = $params['draw_num_1'];
        }
        if(!empty($params['draw_num_2'])) {
            $upData['draw_num_2'] = $params['draw_num_2'];
        }
        if(!empty($params['draw_img_1a'])) {
            $upData['draw_img_1a'] = $params['draw_img_1a'];
        }
        if(!empty($params['draw_img_1b'])) {
            $upData['draw_img_1b'] = $params['draw_img_1b'];
        }
        if(!empty($params['draw_img_2a'])) {
            $upData['draw_img_2a'] = $params['draw_img_2a'];
        }
        if(!empty($params['draw_img_2b'])) {
            $upData['draw_img_2b'] = $params['draw_img_2b'];
        }
        if(isset($params['full_rate'])) {
            $upData['full_rate'] = $params['full_rate'];
        }

        if(empty($upData)) {
            return 0;
        }

        $res = DB::table($this->table)->update($upData);

        $cacheKey = $this->colorGiftConfigKey;
        Redis::del($cacheKey);

        return $res;
    }

    //新增彩蛋奖池
    public function addColorGiftPool($giftId, $poolName, $rate, $minNum, $maxNum, $hours)
    {
        $data = [];
        $data['gift_id'] = $giftId;
        $data['pool_name'] = $poolName;
        $data['min_count'] = $minNum;
        $data['max_count'] = $maxNum;
        $data['is_open'] = 2;
        $data['out_rate'] = $rate;
        $data['hours'] = $hours;
        $data['created_at'] = Carbon::now()->toDateTimeString();

        $id = DB::table($this->colorGiftPoolTable)->insertGetId($data);

        $cacheKey = $this->colorGiftPoolKey;
        Redis::del($cacheKey);
        Redis::del($this->colorGiftPoolByGiftIdKey.$giftId);

        return $id;
    }

    //编辑彩蛋奖池
    public function editColorGiftPool($id, $params)
    {
        $upData = [];
        if(!empty($params['is_open'])) {
            $upData['is_open'] = $params['is_open'];
        }
        if(!empty($params['pool_name'])) {
            $upData['pool_name'] = $params['pool_name'];
        }
        if(!empty($params['min_count'])) {
            $upData['min_count'] = $params['min_count'];
        }
        if(!empty($params['max_count'])) {
            $upData['max_count'] = $params['max_count'];
        }
        if(!empty($params['rate'])) {
            $upData['out_rate'] = $params['rate'];
        }
        if(!empty($params['hours'])) {
            $upData['hours'] = $params['hours'];
        }

        if(empty($upData)) {
            return 0;
        }

        $res = DB::table($this->colorGiftPoolTable)->where('id', $id)->update($upData);

        //奖池下概率配置缓存
        $rateKey = $this->colorGiftPoolConfigKey.$id;
        $cacheKey = $this->colorGiftPoolKey;
        //根据奖池id查询对应礼物
        $gifPoolInfo = DB::table($this->colorGiftPoolTable)->where('id',$id)->first();
        $giftPoolInfo = self::objToArray($gifPoolInfo);
        //删除魔法屋礼物缓存
        Redis::del($this->activityColorConfig);
        Redis::del($this->colorGiftPoolByGiftIdKey.$giftPoolInfo['gift_id']);
        Redis::del($cacheKey);
        Redis::del($rateKey);

        return $res;
    }

    //新增奖池概率配置列表
    public function addPoolRateConfig($data){
        $id = DB::table($this->colorGiftPoolRateConfigTable)->insertGetId($data);

        return $id;
    }

    //新增彩蛋奖池配置
    public function addColorGiftPoolConfig($poolId, $itemNo)
    {
        $data = [];
        $data['pool_id'] = $poolId;
        $data['item_no'] = $itemNo;
        $data['rate_num'] = 1;

        $id = DB::table($this->colorGiftPoolConfigTable)->insertGetId($data);

        return $id;
    }

    //获取目前对应奖池下的最大概率id
    public function getMaxRateNum($poolId){
        $res = DB::table($this->colorGiftPoolConfigTable)->selectRaw(DB::raw('MAX(rate_num) as rate_num'))->where('pool_id',$poolId)->first();
        $res = self::objToArray($res);
        return $res;
    }

    //新增奖池下概率配置
    public function addPoolRate($data){
        $res = DB::table($this->colorGiftPoolConfigTable)->insertGetId($data);
        $cacheKey = $this->colorGiftPoolConfigKey.$data['pool_id'];
        $nowRate = Redis::get($this->colorGiftPoolRateNum.$data['pool_id']);
        $cacheRateKey = $this->colorGiftPoolConfigKey.$data['pool_id'].'-'.$nowRate;
        $colorEggRedis = $this->activityColorConfig;

        Redis::del($colorEggRedis);
        Redis::del($cacheKey);
        Redis::del($cacheRateKey);
        return $res;
    }

    //编辑彩蛋奖池下概率配置
    public function editColorGiftPoolConfig($poolId, $id, $upData)
    {

        $res = DB::table($this->colorGiftPoolConfigTable)->where('id', $id)->update($upData);
        
        $cacheKey = $this->colorGiftPoolConfigKey.$poolId;
        $nowRate = Redis::get($this->colorGiftPoolRateNum.$poolId);
        // 使用Redis的KEYS命令获取所有匹配的键
        $keys = Redis::Keys($this->colorGiftPoolConfigKey.$poolId.'*');
        
        // 删除所有匹配的键
        foreach ($keys as $key) {
            Redis::del($key);
        }
        
        $cacheRateKey = $this->colorGiftPoolConfigKey.$poolId.'-'.$nowRate;
        $colorEggRedis = $this->activityColorConfig;

        Redis::del($colorEggRedis);
        Redis::del($cacheKey);
        Redis::del($cacheRateKey);

        return $res;
    }

    //删除奖池概率
    public function colorGiftRateDel($poolId, $rateNum){
        //若此次删除是奖池中最后一个类型则不允许删除
        $check = DB::table($this->colorGiftPoolConfigTable)
                    ->where('pool_id', $poolId)
                    ->where('is_delete',0)
                    ->where('rate_num','!=',$rateNum)
                    ->count();
        if($check == 0){
            return false;
        }
        $res = DB::table($this->colorGiftPoolConfigTable)->where('pool_id', $poolId)->where('rate_num',$rateNum)->update(['is_delete'=>1]);

        $cacheKey = $this->colorGiftPoolConfigKey.$poolId;
        $cacheRateKey = $this->colorGiftPoolConfigKey.$poolId.'-'.$rateNum;
        $colorEggRedis = $this->activityColorConfig;
        Redis::del($colorEggRedis);
        Redis::del($cacheKey);
        Redis::del($cacheRateKey);
        return $res;
    }

    /**
     * 获取彩蛋配置
     */
    public function getColorGiftConfig(){
        $key = $this->colorGiftConfigKey;
        $list = Redis::get($key);
        if(empty($list)){
            $list = DB::table($this->table)->first();
            $list = self::objToArray($list);
            if(!empty($list)) {
                Redis::setex($key, 86400, json_encode($list));
            }
        }else{
            $list = json_decode($list, true);
        }

        return $list;
    }

    /**
     * 获取彩蛋奖池
     */
    public function getColorGiftPool(){
        $key = $this->colorGiftPoolKey;
        $list = Redis::get($key);
        if(empty($list)){
            $list = DB::table($this->colorGiftPoolTable)->get();
            $list = self::objToArray($list);
            if(!empty($list)) {
                Redis::setex($key, 86400, json_encode($list));
            }
        }else{
            $list = json_decode($list, true);
        }

        $list = array_column($list, null, 'id');

        return $list;
    }

    /**
     * 根据礼物id获取礼物奖池
     */
    public function getColorGiftPoolByGiftId($giftId){
        $key = $this->colorGiftPoolByGiftIdKey.$giftId;
        $list = Redis::get($key);
        if(empty($list)){
            $list = DB::table($this->colorGiftPoolTable)->where('gift_id',$giftId)->where('is_open',1)->get();
            $list = self::objToArray($list);
            if(!empty($list)) {
                Redis::setex($key, 86400, json_encode($list));
            }
        }else{
            $list = json_decode($list, true);
        }

        $list = array_column($list, null, 'id');

        return $list;
    }

    /**
     * 获取彩蛋奖池下的概率V2
     */
    public function getColorGiftPoolConfigV2($poolId, $rateNum){
        $key = $this->colorGiftPoolConfigKey.$poolId.'-'.$rateNum;
        $list = Redis::get($key);
        if(empty($list)){
            $list = DB::table($this->colorGiftPoolConfigTable)->where('pool_id', $poolId)->where('is_delete',0)->where('rate_num',$rateNum)->get();
            $list = self::objToArray($list);
            if(!empty($list)) {
                Redis::setex($key, 86400, json_encode($list));
            }
        }else{
            $list = json_decode($list, true);
        }

        return $list;
    }

    /**
     * 获取彩蛋奖池配置
     */
    public function getColorGiftPoolConfig($poolId){
        $key = $this->colorGiftPoolConfigKey.$poolId;
        $list = Redis::get($key);
        if(empty($list)){
            $list = DB::table($this->colorGiftPoolConfigTable)->where('pool_id', $poolId)->where('is_delete',0)->get();
            $list = self::objToArray($list);
            if(!empty($list)) {
                Redis::setex($key, 86400, json_encode($list));
            }
        }else{
            $list = json_decode($list, true);
        }

        return $list;
    }

    /**
     * 根据奖池id获取彩蛋id
     */
    public function getColorGiftPoolInfoByPooId($poolId){
        $res = DB::table($this->colorGiftPoolTable)->where('id',$poolId)->first();
        $res = self::objToArray($res);
        return $res;
    }

    /**
     * 初始化彩蛋奖池配置数据缓存
     */
    public function makeColorGiftPoolData($poolId, $data, $rateNum){
        $key = sprintf($this->colorGiftPoolDataKey, $poolId.'-'.$rateNum);
        aliasInit($key, $data);
    }

    /**
     * 彩蛋奖池配置剩余数量
     */
    public function getColorGiftPoolData($poolId, $rateNum=0){
        $result = [];
        $nowRateNum = Redis::GET($this->colorGiftPoolRateNum.$poolId);
        if(empty($nowRateNum)){
            return [];
        }
        $key = sprintf($this->colorGiftPoolDataKey, $poolId.'-'.$rateNum);
        $nameKey = $key . ':ALIAS:' . 'KEYNAME';
        $valKey = Redis::GET($nameKey);
        if(!empty($valKey)) {
            if(Redis::EXISTS($valKey)) {
                $list = Redis::LRANGE($valKey, 0, -1);
                if(!empty($list)) {
                    foreach($list as $val) {
                        if(empty($result[$val])) {
                            $result[$val]['id'] = $val;
                            $result[$val]['num'] = 0;
                        }
                        $result[$val]['num'] = $result[$val]['num'] + 1;
                    }
                }
            }
        }
        $result = array_values($result);

        return $result;
    }

    /**
     * 用户总抽奖记录数
     */
    public function getUserColorGiftDrawNum($userId, $cache=0)
    {
        $num = 0;
        $cacheKey = $this->userDrawNumKey.$userId;
        if($cache) {
            $num = Redis::get($cacheKey);
        }
        if(empty($num)) {
            $query = DB::table($this->wOrderTable)
                ->where('user_id', $userId);
            $num = $query->sum('gift_num');
        }
        $num = (int) $num;

        Redis::set($cacheKey, $num);
        Redis::expire($cacheKey, 3600);

        return $num;
    }

    //累加用户总抽奖记录数
    public function incrUserDrawNum($userId, $num)
    {
        if($num <= 0) {
            return false;
        }
        $cacheKey = $this->userDrawNumKey.$userId;
        $new = Redis::INCRBY($cacheKey, $num);

        return $new;
    }

    /**
     * 获取用户白名单
     */
    public function getColorGiftWhite($userId){
        $list = DB::table($this->wWhiteTable)
            ->where('user_id', $userId)
            ->where('status', 0)
            ->where('is_delete', 0)
            ->get();
        $list = self::objToArray($list);

        return $list;
    }

    //新增彩蛋订单记录
    public function addColorGiftOrder($data)
    {
        $id = DB::table($this->wOrderTable)->insertGetId($data);

        return $id;
    }

    //彩蛋自指定奖池抽取
    public function drawColorGift($poolId)
    {
        $roundNum = Redis::get($this->colorGiftPoolRateNum.$poolId);
        if(!empty($roundNum)){
            $key = sprintf($this->colorGiftPoolDataKey, $poolId.'-'.$roundNum);
        }else{
            $key = sprintf($this->colorGiftPoolDataKey, $poolId);
        }
        $poolConfigId = aliasIndex($key);

        return $poolConfigId;
    }

    //新增彩蛋抽奖记录
    public function addColorGiftDraw($data)
    {
        $id = DB::table($this->wDrawTable)->insertGetId($data);

        return $id;
    }

    //更新彩蛋订单记录
    public function updateColorGiftDraw($orderId, $rewardDiamondNum)
    {
        $res = DB::table($this->wOrderTable)->where('id', $orderId)->update(['reward_diamond_num'=>$rewardDiamondNum]);

        return $res;
    }

    //更新彩蛋白名单
    public function updateColorGiftWhite($id, $orderId)
    {
        $res = DB::table($this->wWhiteTable)->where('id', $id)->update(['status'=>1, 'order_id'=>$orderId]);

        return $res;
    }

    /**
     * 彩蛋中奖记录列表
     */
    public function awardList($orderId, $limit, $where){
        $query = DB::table($this->wOrderTable);
        if($orderId > 0) {
            $query = $query->where('id', '<', $orderId);
        }
        if(!empty($where['user_id'])) {
            $query = $query->where('user_id', $where['user_id']);
        }
        if(!empty($where['created_at_st'])) {
            $query = $query->where('created_at', '>=', $where['created_at_st']);
        }
        $list = $query->limit($limit)->orderBy('id', 'desc')->get();
        $list = self::objToArray($list);

        $result = [];
        if(!empty($list)) {
            $orderIds = array_column($list, 'id');
            $drawList = DB::table($this->wDrawTable)
                ->whereIn('order_id', $orderIds)
                ->selectRaw('sum(gift_num) as gift_num, order_id, gift_id, user_id')
                ->groupBy('order_id', 'gift_id')
                ->get();
            $drawList = self::objToArray($drawList);

            $list = array_column($list, null, 'id');

            foreach($drawList as $key=>$val) {
                $val['created_at'] = $list[$val['order_id']]['created_at'];
                $drawList[$key] = $val;
            }

            array_multisort(array_column($drawList, 'order_id'), SORT_DESC, $drawList);

            $result = $drawList;
        }
        return $result;
    }

    //新增彩蛋白名单
    public function addColorGiftWhite($userId, $giftId)
    {
        $data = [];
        $data['user_id'] = $userId;
        $data['gift_id'] = $giftId;
        $data['status'] = 0;

        $id = DB::table($this->wWhiteTable)->insertGetId($data);

        return $id;
    }

    //删除彩蛋白名单
    public function delColorGiftWhite($whiteId)
    {
        $id = DB::table($this->wWhiteTable)->where('id', $whiteId)->update(['is_delete'=>1]);

        return $id;
    }

    //统计彩蛋数据
    public function colorGiftStatistics($dateSt, $dateEt)
    {
        $info = DB::table($this->wOrderTable)
            ->where('created_at', '>=', $dateSt)
            ->where('created_at', '<=', $dateEt)
            ->selectRaw('count(DISTINCT user_id) as user_num, sum(num) as draw_num, sum(prop_num) as prop_num, sum(prop_diamond_num) as prop_diamond_num, sum(reward_diamond_num) as reward_diamond_num')
            ->first();
        $info = self::objToArray($info);

        return $info;
    }

    //新增彩蛋每日统计
    public function addColorGiftStatistics($data)
    {
        $id = DB::table($this->wStatisticsTable)->insertGetId($data);

        return $id;
    }

    /**
     * 彩蛋订单中奖记录列表
     */
    public function orderAwardList($orderId){
        $info = DB::table($this->wOrderTable)->where('id', $orderId)->first();
        $info = self::objToArray($info);
        if(empty($info)) {
            return [];
        }

        $drawList = DB::table($this->wDrawTable)
            ->where('order_id', $orderId)
            ->selectRaw('sum(gift_num) as gift_num, sum(prop_diamond_num) as prop_diamond_num, sum(reward_diamond_num) as reward_diamond_num, gift_id')
            ->groupBy('gift_id')
            ->get();
        $drawList = self::objToArray($drawList);
        if(empty($drawList)) {
            return [];
        }
        $info['drawList'] = $drawList;

        return $info;
    }

    //新增概率banner
    public function colorGiftRateAddBanner($data){
        $res = DB::table($this->colorGiftPoolRateBannerTable)->insert($data);
        return $res;
    }
    
    //编辑概率banner
    public function colorGiftRateEditBanner($data, $id){
        $res = DB::table($this->colorGiftPoolRateBannerTable)
                ->where('id',$id)
                ->update($data);
        Redis::del($this->colorGiftRateBanner.$data['pool_id'].':'.$data['rate_num']);
        return $res;
    }

    //查询banner
    public function getColorGiftRateBanner($poolId, $rateNum){
        $res = DB::table($this->colorGiftPoolRateBannerTable)
                ->where('pool_id',$poolId)
                ->where('rate_num',$rateNum)
                ->first();
        $res = self::objToArray($res);
        return $res;
    }

    //后台新增白名单用户
    public function colorGiftWhiteAdd($data){
        $res = DB::table($this->colorWhiteConfig)->insertGetId($data);
        return $res;
    }

    //删除彩蛋白名单配置
    public function colorGiftWhiteUpdate($data, $id){
        $res = DB::table($this->colorWhiteConfig)->where('id',$id)->update($data);
        return $res;
    }






    /**
     **************************************** API接口 **************************************************
     */
    //api根据礼物id获取对应开启的池子id下的概率banner
    public function getColorGiftRateBannerByGiftId($giftId){
        //获取礼物下开启的池子id
        $pooList = $this->getColorGiftPoolByGiftId($giftId);
        if(empty($pooList)){
            return [];
        }
        //查询奖池配置取最大配置数
        $poolArr = array_column($pooList,'max_count','id');
        $maxKey = array_search(max($poolArr), $poolArr);
        $maxPoolId = $maxKey;//最大奖池id
        //根据奖池id查询概率图片
        $maxPoolIdRateNum = Redis::get($this->colorGiftPoolRateNum.$maxPoolId);
        //查询banner
	$bannerInfo = Redis::get($this->colorGiftRateBanner.$maxPoolId.':'.$maxPoolIdRateNum);
        
        if(empty($bannerInfo)){
            $bannerInfo = $this->getColorGiftRateBanner($maxPoolId, $maxPoolIdRateNum);
            if(empty($bannerInfo)){
                return [];
            }
            Redis::set($this->colorGiftRateBanner.$maxPoolId.':'.$maxPoolIdRateNum,json_encode($bannerInfo));
            Redis::EXPIRE($this->colorGiftRateBanner.$maxPoolId.':'.$maxPoolIdRateNum,86400);
        }else{
            $bannerInfo = json_decode($bannerInfo,true);
        }
        return $bannerInfo;
    }

    //获取彩蛋白名单
    public function getColorGiftWhiteInfo($userId, $gift_id){
        $res = DB::table($this->colorWhiteConfig)
                ->where('user_id',$userId)
                ->where('color_gift_id', $gift_id)
                ->where('status',0)
                ->where('is_deleted',0)
                ->get();
        $res = self::objToArray($res);

        return $res;
    }

    //检查白名单的礼物是否在池子中
    public function checkPopGift($gift_id, $poolId){
        $roundNum = Redis::get($this->colorGiftPoolRateNum.$poolId);
        $poolList = $this->getColorGiftPoolConfigV2($poolId, $roundNum);
        if(empty($poolList)) {
            return [];
        }

        $hasList = $this->getColorGiftPoolData($poolId, $roundNum);
        if(empty($hasList)){
            return [];
        }
        $hasList = array_column($hasList, 'num', 'id');

        foreach($poolList as $key=>$val) {
            $val['less_num'] = empty($hasList[$val['id']]) ? 0 : $hasList[$val['id']];
            $val['rate_id'] = $val['id'];
            $poolList[$key] = $val;
        }
        $poolCount = array_column($poolList,'less_num','gift_id');
        $poolRateId = array_column($poolList,'rate_id','gift_id');
        if(isset($poolCount[$gift_id]) && $poolCount[$gift_id] > 0){
            return $poolRateId[$gift_id];
        }
        return false;
    }

    //检查白名单的礼物是否在池子中不从奖池扣除
    public function checkPopGiftNotLimit($gift_id, $poolId){
        $roundNum = Redis::get($this->colorGiftPoolRateNum.$poolId);
        $poolList = $this->getColorGiftPoolConfigV2($poolId, $roundNum);
        if(empty($poolList)) {
            return [];
        }

        $hasList = $this->getColorGiftPoolData($poolId, $roundNum);
        if(empty($hasList)){
            return [];
        }
        $hasList = array_column($hasList, 'num', 'id');

        foreach($poolList as $key=>$val) {
            $val['less_num'] = empty($hasList[$val['id']]) ? 0 : $hasList[$val['id']];
            $val['rate_id'] = $val['id'];
            $poolList[$key] = $val;
        }
        $poolCount = array_column($poolList,'less_num','gift_id');
        $poolRateId = array_column($poolList,'rate_id','gift_id');
        $poolAllNum = array_column($poolList,null,'gift_id');
        if(isset($poolCount[$gift_id]) && $poolCount[$gift_id] > 0){
            return $poolRateId[$gift_id];
        }elseif(isset($poolCount[$gift_id]) && $poolCount[$gift_id] == 0 && $poolAllNum[$gift_id]['gift_num'] >0){
            return $poolRateId[$gift_id];
        }
        return false;
    }

    //减去池子中的礼物
    public function delPoolGift($poolId, $rateId){
        $roundNum = Redis::get($this->colorGiftPoolRateNum.$poolId);
        if(!empty($roundNum)){
            $key = sprintf($this->colorGiftPoolDataKey, $poolId.'-'.$roundNum);
        }else{
            $key = sprintf($this->colorGiftPoolDataKey, $poolId);
        }
        $nameKey = $key . ':ALIAS:' . 'KEYNAME';
        $valKey = Redis::GET($nameKey);
        if(!empty($valKey)) {
            if(Redis::EXISTS($valKey)) {
                Redis::LREM($valKey,'-1',$rateId);//移除其中的礼物
                $nowUse = explode(':',$valKey);
                $nowNum = $nowUse[4];
                if($nowNum == 0){
                    Redis::RPUSH($key.':ALIAS:1',$rateId);
                }else{
                    Redis::RPUSH($key.':ALIAS:0',$rateId);
                }
            }
        }
    }

    //更新白名单用户状态
    public function upWhiteUserInfo($id){
        $data = [
            'status' => 1,
            'updated_at' => Carbon::now()->toDateTimeString(),
        ];
        $res = DB::table($this->colorWhiteConfig)->where('id',$id)->update($data);
        return $res;
    }
}
